package com.joint.web.action.app;

import com.fz.us.base.util.DataUtil;
import com.fz.us.dict.bean.DictBean;
import com.fz.us.dict.entity.Dict;
import com.fz.us.dict.service.DictService;
import com.joint.web.action.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台Action类 - 系统字典控制Action
 * ============================================================================
 * 版权所有 2013 。
 * ----------------------------------------------------------------------------
 * 
 * @author fallenpanda
 * 
 * @version 0.1 2013-1-16
 * @version 2.0 2014-12-24 by fallenpanda
 */
@ParentPackage("app")
public class DictAction extends BaseAction {

	private static final long serialVersionUID = -5383463207248344967L;

    @Resource
    private DictService dictService;

	// 系统字典
	public String getDictList() throws Exception {
		users = getRequestUsers();
        company = users.getCompany();
        if(company == null)
            return ajaxHtmlAppResult(0, "未找到用户所属企业！",null);

        //字典类型
        if (StringUtils.isNotEmpty(keyId)) {
            List<Dict> dictList =dictService.listFormDefinedEnable(DictBean.DictEnum.valueOf(keyId), company.getId());
            List<JSONObject> dataRows = new ArrayList<JSONObject>();
            Map<String, Object> data = new HashMap<String, Object>();

            for (Dict d : dictList) {
                Map<String, Object> rMap = new HashMap<String, Object>();
                rMap.put("id", d.getId());
                rMap.put("no", d.getNo());
                rMap.put("name", d.getSname());
                rMap.put("value", StringUtils.isNotEmpty(d.getValue())?d.getValue():"");
                rMap.put("createDate", DataUtil.DateTimeToString(d.getCreateDate()));

                dataRows.add(JSONObject.fromObject(rMap));
            }
            data.put("keyWord", keyId);
            data.put("dataRows", JSONArray.fromObject(dataRows));
            return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
        } else {
            return ajaxHtmlAppResult(0, "请选择字典类型！", null);
        }
	}

}